package com.mgetwd.springbootweb.utils;


import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class SecureUtils {
    /**
     * 加密 content机密内容
     * key对应key
     */
    public static String encryptHex(JSON content, byte[] key) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        return aes.encryptHex(content.toJSONString(0));

    }

    /**
     * 解密  content需要解密的内容
     * key对应key
     */
    public static JSONObject decryptStr(String content, byte[] key) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
        return JSONUtil.parseObj(aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8));

    }

    /**
     * 生成随机的key
     */
    public static byte[] keys() {
        return SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
    }

    public static void main(String[] args) throws Exception {

    }
}
